/*
 ============================================================================
 Name		: MainContainer.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CMainContainer implementation
 ============================================================================
 */

#include <coemain.h>
#include "MainContainer.h"

#include <aknlists.h>
#include <gulicon.h>
#include <AknIconUtils.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <eikapp.h>
#include <aknquerydialog.h>
#include <StringLoader.h>

#include "LemonAssistantAppUi.h"
#include "LemonAssistant.hrh"

#include <LemonAssistant.mbg>
#include <LemonAssistant.rsg>

#include "LMSvgUtil.h"


// Format string for listbox items

CMainContainer::CMainContainer()
:iListBox(NULL)
	{
	// No implementation required
	}

CMainContainer::~CMainContainer()
	{
    if( iListBox ) delete iListBox;
	}

CMainContainer* CMainContainer::NewLC(const TRect& aRect)
	{
	CMainContainer* self = new (ELeave) CMainContainer();
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

CMainContainer* CMainContainer::NewL(const TRect& aRect)
	{
	CMainContainer* self = CMainContainer::NewLC(aRect);
	CleanupStack::Pop(); // self;
	return self;
	}

void CMainContainer::ConstructL(const TRect& aRect)
	{
	CreateWindowL();
	SetRect(aRect);
    CreateListboxL();

    iListBox->SetRect(Rect());

    // Set lisbox icons
    SetIconsL();
    // Set listbox item texts
    SetItemsL();
    // Activates window. ( Ready to draw )
    ActivateL();

	}

// ---------------------------------------------------------
// CMainContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CMainContainer::CountComponentControls() const
	{
	TInt count( 0 );	
	if ( iListBox )
		count++;
	
	return count; // return nbr of controls inside this container
	}

// ---------------------------------------------------------
// CMainContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CMainContainer::ComponentControl(TInt aIndex) const
	{
	switch (aIndex)
		{
		case 0:
			return iListBox; // Returns the pointer to listbox object.
		default:
			return NULL;
		}
	}

// ---------------------------------------------------------
// CMainContainer::HandleControlEventL(
//	 CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CMainContainer::HandleControlEventL(CCoeControl* /*aControl*/, TCoeEvent /*aEventType*/)
	{
	// TODO: Add your control event handler code here
	}

//------------------------------------------------------------------
//CMainContainer::OfferKeyEventL(
// const TKeyEvent& aKeyEvent,	TEventCode aType)
//-------------------------------------------------------------------
//
TKeyResponse CMainContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
		TEventCode aType)
	{
	// See if we have a selection
	TInt code = aKeyEvent.iCode;
	switch (code)
		{
		//add your code here...
		case EKeyDownArrow:
		case EKeyUpArrow:
			return iListBox->OfferKeyEventL( aKeyEvent, aType );
//		case EKeyDevice3:
//			return PressSelected();
		default:
			break;
		}
	return EKeyWasNotConsumed;
	}

TKeyResponse CMainContainer::PressSelected()
	{
	TInt index = iListBox->View()->CurrentItemIndex();
	switch (index)
		{
		case 0:
			STATIC_CAST(CLemonAssistantAppUi*,CEikonEnv::Static()->AppUi())
			->ActivateLocalViewL(TUid::Uid(ELemonAssistantAppViewStrLight));
			break;
		case 1:
			STATIC_CAST(CLemonAssistantAppUi*,CEikonEnv::Static()->AppUi())
			->ActivateLocalViewL(TUid::Uid(ELemonAssistantAppViewFlashLight));
			break;
		case 2:
			STATIC_CAST(CLemonAssistantAppUi*,CEikonEnv::Static()->AppUi())
			->ActivateLocalViewL(TUid::Uid(ELemonAssistantAppViewVibraMassager));
			break;
		}
	return EKeyWasConsumed;
	}

void CMainContainer::Selected()
	{
	TInt index = iListBox->View()->CurrentItemIndex();
	switch (index)
		{
		case 0:
			STATIC_CAST(CLemonAssistantAppUi*,CEikonEnv::Static()->AppUi())
			->ActivateLocalViewL(TUid::Uid(ELemonAssistantAppViewStrLight));
			break;
		case 1:
			STATIC_CAST(CLemonAssistantAppUi*,CEikonEnv::Static()->AppUi())
			->ActivateLocalViewL(TUid::Uid(ELemonAssistantAppViewFlashLight));
			break;
		case 2:
			STATIC_CAST(CLemonAssistantAppUi*,CEikonEnv::Static()->AppUi())
			->ActivateLocalViewL(TUid::Uid(ELemonAssistantAppViewVibraMassager));
			break;
		}
	}
// -----------------------------------------------------------------------------
// CMainContainer::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CMainContainer::Draw(const TRect& /*aRect*/) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();
	TRect drawRect(Rect());
	gc.Clear(drawRect);

	//add your code here...
	}

// -----------------------------------------------------------------------------
// CMainContainer::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CMainContainer::SizeChanged()
	{
    if ( iListBox )
        {
        iListBox->SetRect( Rect() );
        }
	DrawNow();
	}
// End of File

// -----------------------------------------------------------------------------
// CListboxGraphicContainer::AddItemL()
// Adds item to the listbox.
// -----------------------------------------------------------------------------
//
void CMainContainer::AddItemL()
    {
    // Icon number for all items.
//    _LIT( KItemStart, "0\t" );
//    // New item text
//    TBuf<50> itemText;
//    CAknTextQueryDialog *dialog = CAknTextQueryDialog::NewL( itemText );
//    // Maximum length for input text. Can also be defined in
//    // R_LISTBOX_DATA_QUERY_DIALOG resourse.
//    dialog->SetMaxLength( 45 );
//    // Execute dialog
//    if (dialog->ExecuteLD( R_LISTBOX_DATA_QUERY_DIALOG ) )
//        {
//        // Insert icon number at the beginning of itemText. Note that max
//        // length for dialog input text is 45 and itemText size is 50. So
//        // there is space for item number and column separator.
//        itemText.Insert( 0, KItemStart() );
//        // Item array. By default iListBox owns the item array, so no
//        // deletion is needed.
//        CDesCArray* itemArray =
//            static_cast<CDesC16ArrayFlat*>(iListBox->Model()->ItemTextArray());
//
//        // Insert item to the item array
//        if( itemArray->Count() > 0 )
//            itemArray->InsertL( iListBox->View()->CurrentItemIndex(),itemText );
//        else
//            itemArray->InsertL( 0, itemText );
//
//        // Update the listbox
//        iListBox->HandleItemAdditionL();
//        }
    }

// -----------------------------------------------------------------------------
// CMainContainer::RemoveItemL()
// Removes focused item from the listbox.
// -----------------------------------------------------------------------------
//
void CMainContainer::RemoveItemL()
    {
    // If we don't have items, do nothing and return
    if( iListBox->Model()->NumberOfItems() < 1 )
        return;

    TInt oldIndex = iListBox->View()->CurrentItemIndex();
    // Item array. By default iListBox owns the item array, so no deletion is
    // needed.
    CDesCArray* itemArray =
        static_cast<CDesC16ArrayFlat*>(iListBox->Model()->ItemTextArray());

    // Remove highlighted item from the listbox.
    itemArray->Delete( iListBox->View()->CurrentItemIndex() );

    // If focused item is out of range, bring it forth
    if( (oldIndex > ( itemArray->Count()-1 )) &&
        ( (itemArray->Count()-1) >= 0 ) )
        {
        iListBox->View()->SetCurrentItemIndex( itemArray->Count()-1 );
        }

    // Update the listbox
    iListBox->HandleItemAdditionL();
    }

// -----------------------------------------------------------------------------
// CMainContainer::ItemCount()
// Getter for listbox item count.
// -----------------------------------------------------------------------------
//
TInt CMainContainer::ItemCount()
    {
    return iListBox->Model()->NumberOfItems();
    }

// -----------------------------------------------------------------------------
// CMainContainer::CreateListboxL()
// Constructs listbox, creates scroll bar, sets empty list background text, and
// enables marquee.
// -----------------------------------------------------------------------------
//
void CMainContainer::CreateListboxL()
    {
    iListBox = new ( ELeave ) CAknSingleLargeStyleListBox();
    iListBox->ConstructL(this);
    iListBox->SetContainerWindowL( *this );

    // Creates scrollbar.
    iListBox->CreateScrollBarFrameL( ETrue );
    iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
        CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOff );

    // Enable marquee
    iListBox->ItemDrawer()->ColumnData()->EnableMarqueeL( ETrue );

    // Set empty text for the listbox.
//    HBufC* emptyText = StringLoader::LoadLC( R_LISTBOX_EMPTYTEXT );
//    iListBox->View()->SetListEmptyTextL( emptyText->Des() );
//    CleanupStack::PopAndDestroy( emptyText );
    }

// -----------------------------------------------------------------------------
// CMainContainer::SetIconsL()
// Sets icons for the listbox.
// -----------------------------------------------------------------------------
//
void CMainContainer::SetIconsL()
    {
    CArrayPtr<CGulIcon>* iconList = new (ELeave) CAknIconArray(1);
    // push iconarray to stack
    CleanupStack::PushL(iconList);

    
//    CFbsBitmap* bitmap1 = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantIcon1);
//    CFbsBitmap* mask1 = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantIcon1mask);    
    CFbsBitmap *bitmap1 ,*mask1;
    LMSvgUtil::GetImageAndMaskFormResourceL(bitmap1,mask1,EMbmLemonassistantIcon1,EMbmLemonassistantIcon1_mask);
    CGulIcon* newIcon1 = CGulIcon::NewL( bitmap1, mask1 );
    iconList->AppendL( newIcon1 );
    
//    CFbsBitmap* bitmap2 = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantIcon2);
//    CFbsBitmap* mask2 = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantIcon2mask);
    CFbsBitmap *bitmap2 ,*mask2;
    LMSvgUtil::GetImageAndMaskFormResourceL(bitmap2,mask2,EMbmLemonassistantIcon2,EMbmLemonassistantIcon2_mask);
    CGulIcon* newIcon2 = CGulIcon::NewL( bitmap2, mask2 );
    iconList->AppendL( newIcon2 );
    
//    CFbsBitmap* bitmap3 = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantIcon3);
//    CFbsBitmap* mask3 = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantIcon3mask);
    CFbsBitmap *bitmap3 ,*mask3;
    LMSvgUtil::GetImageAndMaskFormResourceL(bitmap3,mask3,EMbmLemonassistantIcon3,EMbmLemonassistantIcon3_mask);
    CGulIcon* newIcon3 = CGulIcon::NewL( bitmap3, mask3 );
    iconList->AppendL( newIcon3 );

    iListBox->ItemDrawer()->ColumnData()->SetIconArray( iconList );
    CleanupStack::Pop(iconList);
    }

// -----------------------------------------------------------------------------
// CMainContainer::SetItemsL()
// Sets items to the listbox.
// -----------------------------------------------------------------------------
//
void CMainContainer::SetItemsL()
    {
    CDesCArray* itemArray = 
		iCoeEnv->ReadDesCArrayResourceL(R_FUNCTION_LABELS);

    iListBox->Model()->SetItemTextArray( itemArray );
    iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray );
    iListBox->HandleItemAdditionL();
    }

void CMainContainer::HandleResourceChange( TInt aType )
    {
    CCoeControl::HandleResourceChange( aType );
    if  ( aType == KEikDynamicLayoutVariantSwitch )
    	{
        TRect rect;
        AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
        SetRect(rect);
        }
    }
